<?php
class xta {
	var $filename;
	var $data;
	
	public function load($file_name) {
		if(! file_exists('data/'.$file_name.".xml"))
			die('File Not Found!');
			
		$this->data = $this->readData($file_name);
		return true;		
	}
	
	public function saveAs($tagname, $pk='') {
		if(empty($this->filename))
			$this->filename = $tagname; 
			
		$this->saveData($this->data, $tagname, $pk, false);
		return true;
	}
	
	public function save($tagname, $pk='') {
		if(empty($this->filename))
			$this->filename = $tagname; 
			
		$this->saveData($this->data, $tagname, $pk);
		return true;
	}
	
	public function makeArray($nodes, &$data, $key=0) {
			
		foreach($nodes as $each_node) {			
			if($each_node->hasChildNodes()) {
				if($each_node->childNodes->length > 1) {
					$key = $each_node->nodeName."s";					
					$this->makeArray($each_node->childNodes, $data[$key][], $each_node->nodeName);
				} else {
					$data[$each_node->nodeName] = $each_node->nodeValue;						
				}
			}
		}		
	}
	
	public function saveData($data, $tagname, $tagid = "", $append=true) {
		
		$filename = $this->filename;
		
		$previous_value = libxml_use_internal_errors(TRUE);
	
		$doc = new DOMDocument();
		$datafile = 'data/'.$filename.".xml";
					
	  	if(file_exists($datafile)) {
	  		$doc->load($datafile); 
			$r = $doc->getElementsByTagName($tagname)->item(0);
			if(! $append) {
				$doc->removeChild($doc->documentElement);
				$r = $doc->createElement($tagname);
				$doc->appendChild( $r );	
			}
	  	} else {
			$r = $doc->createElement($tagname);
	  		$doc->appendChild( $r );
		}
		
		$xpath = new DOMXPath( $doc );			
	
	   if(count($data) > 0) {
	
		   $columns = array_keys(current($data));   
	   
	  		foreach( $data as $row ) {
				$rowName = (string) substr($tagname, 0, (strlen($tagname)-1));
		  		$b = $doc->createElement($rowName);
		  		
		  		foreach( $columns as $column ) {
		  			$name = $doc->createElement($column);
		  			$name->appendChild($doc->createTextNode($row[$column]));
	  				$b->appendChild($name);
				}
				
				$expression = '//'.$rowName.'[@'.$tagid.'="'.$row[$tagid].'"]';
				$elements = $xpath->query($expression);
							
				if(empty($elements->length)) {
		  			$newRecord = $r->appendChild($b);
				
					if(!empty($tagid))  		
		  				$newRecord->setAttribute($tagid, $row[$tagid]);
		  		}
	  		}
	  }
	  
	  $doc->saveXML();
	  $doc->formatOutput = true;  
	  $doc->save($datafile);
	  
	  libxml_clear_errors();
	  libxml_use_internal_errors($previous_value);  
	}


	public function readData($file_name, $tag_name='', $key = 0) {
		$previous_value = libxml_use_internal_errors(TRUE);
		
		$data = array();
		
		$doc = new DOMDocument();
		$datafile = 'data/'.$file_name.".xml";
		
		if(file_exists($datafile)) {
			$doc->load($datafile);
			$xpath = new DOMXPath( $doc );
		}
		
		if(!empty($tag_name)) {
			$r = $doc->getElementsByTagName($tag_name);
			$this->makeArray($r, $data, $tag_name);
		} else {
			$r = $doc->documentElement->childNodes;
			$this->makeArray($r, $data);
		}
		
		libxml_clear_errors();
	  	libxml_use_internal_errors($previous_value);
	  
		return $data;
	}
}

//$xta = new xta();
//$xta->load('national');
//print_r($xta->data);
//$xta->save('links');
?>